package com.zzz.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

@Controller
public class SSEController {

    @ResponseBody
    @RequestMapping(value = "/see/get_data", produces = "text/event-stream;charset=UTF-8")
    public String push() {
        try {
            TimeUnit.SECONDS.sleep(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String nowTime = dtf.format(LocalDateTime.now());
        String dataFormat = "data: %s\n\n";
        String result = String.format(dataFormat, "股票行情:" + Math.random() + nowTime);
        return result;
    }

    @GetMapping("/index")
    public String index() {
        return "index";
    }
}